/******************************************
 * title.cpp - the title menu for the 
 * CS 467 game.
 *
 * Should be run before the actual game,
 * will return info for the game start up.
 *
 * Author: Jeremy Zito
 * Date: 1/31/07
 ******************************************/


#include "TitleMenu.h"



TitleMenu::TitleMenu()
{	
	eye_x = 0;
	eye_y = 0;
	eye_z = 20;

	ones[0] = ones[1] = ones[2] = ones[3] = .5;

	//set up the screen resolution array
	screenResolutions[0] = "800 x 600";
	screenResolutions[1] = "1024 x 768";
	screenResolutions[2] = "1280 x 1024";
	screenResolutions[3] = "1600 x 1200";
	curResolution = 1;

	numMoons = 8;

	//init game options
	gi.width = 1024;
	gi.height = 768;
	gi.moons = numMoons;

	//init stars
	numStars = 2000;
	stars = new Star[numStars];

	rot = 0;

	width = 800;
	height = 600;
}

/*----------------------------------------------------------------------------*/
TitleMenu::~TitleMenu()
{
}

/*----------------------------------------------------------------------------*/
GameInitInfo TitleMenu::startTitle()
{		
	//create the title menu window
	createWindow("test", width, height, 0, 0, false);
	

	//set up opengl
	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);   
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);

  
	float l1c[3] = { 250, 250, 250 };
	glLightfv (GL_LIGHT1, GL_AMBIENT, l1c);
	glLightfv (GL_LIGHT1, GL_DIFFUSE, l1c);
	glLightfv (GL_LIGHT1, GL_SPECULAR, l1c);
   
   
    glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
    glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
    glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);

   
	glClearColor(0.0, 0.0, 0.0, 1.0);
	
	//set up th gui
	initGUI();	

	//enter the title menu window main loop
	startWindow();

	//return the game info structure when we are done
	return gi;
}

/*----------------------------------------------------------------------------*/
void TitleMenu::displayFunc()
{
	// clear the window before drawing
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// setup viewing transformation: place the camera/viewer/eye
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();	

	gluLookAt (
      eye_x, eye_y, eye_z, // eye position 
      0.0  , 0.0  , 0.0,   // center of attention 
      0.0  , 1.0  , 0.0);  // which way is UP

	glRotatef(rot, 0, 1, 0);
	rot+=.05;
	
	drawStars();

	gui.draw_GUI();

	swapBuffers();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::drawStars() 
{		

	for ( register unsigned int i = 0 ; i < numStars ; i++ ) {

		glMaterialfv( GL_FRONT, GL_DIFFUSE, stars[i].getDiffuse() );
		glMaterialfv( GL_FRONT, GL_AMBIENT, ones );
		glMaterialfv( GL_FRONT, GL_EMISSION, ones );

		const Vec3f* loc = stars[i].getLoc();
		
		glPointSize((float)stars[i].getSize()); // Must call outside of glBegin/End
		glBegin(GL_POINTS);
			glVertex3f( loc->x, loc->y, loc->z );
		glEnd();
	}
}

/*----------------------------------------------------------------------------*/
void TitleMenu::idleFunc(void)
{
	postRedisplay();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::keyboardFunc(unsigned char key, int x, int y)
{
	gui.check_key_event(key);

	switch (key){

	}

	postRedisplay();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::motionFunc(int x, int y)
{
	gui.check_drag_event(x, y);
	postRedisplay();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::mouseFunc(int button, int state, int x, int y)
{
	int butClicked;
	butClicked = gui.check_click_event(button, state, x, y);
	if (butFuncs.find(butClicked) != butFuncs.end())
		(this->*(butFuncs[butClicked]))((void*)butClicked);
	postRedisplay();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::passiveMotionFunc(int x, int y)
{
}

/*----------------------------------------------------------------------------*/
void TitleMenu::reshapeFunc(int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);

	// setup projection transformation 
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
  
	// choose gluPerspective(), glOrtho(), or gluOrtho2D()
	// Choose the type of camera and lens:
	//    60.0 degrees field of view
	//    depth range: 0.001 - 300.0
	gluPerspective (45.0, (float) w / (float) h, 0.001, 2000.0);

	// setup viewing transformation: place the camera 
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	gluLookAt (
      eye_x, eye_y, eye_z, // eye position 
      0.0  , 0.0  , 0.0,   // center of attention 
      0.0  , 1.0  , 0.0);  // which way is UP 
  
	// when the setup is done, mark the window as needed redisplay 
	postRedisplay();
}

/*----------------------------------------------------------------------------*/
void TitleMenu::initGUI()
{
	gui.init();
	

	int tb_rgb[3] = {100, 100, 100};
	
	int win1_w = 250;
	int win1_h = 200;
	int butid;	

	//main window
	gui.create_window("", "skins/menu1.skn", width/2-win1_w/2, height/2-win1_h/2, win1_w, win1_h);	
	butid = gui.windows[0].add_button("   Join Game", win1_w/2-75, win1_h-50, 155, 20, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::joinGameButtonFunc;
    butid = gui.windows[0].add_button("   Host Game", win1_w/2-75, win1_h-90, 155, 20, "btu.tga", "btd.tga", NULL);  
	   butFuncs[butid] = &TitleMenu::hostGameButtonFunc;
	butid = gui.windows[0].add_button("    Options", win1_w/2-75, win1_h-130, 155, 20, "btu.tga", "btd.tga", NULL); 
	   butFuncs[butid] = &TitleMenu::optionsButtonFunc;
    butid = gui.windows[0].add_button("     Quit", win1_w/2-75, win1_h-170, 155, 20, "btu.tga", "btd.tga", NULL); 
	   butFuncs[butid] = &TitleMenu::quitButtonFunc;
	gui.windows[0].set_visibility(true);
    gui.windows[0].set_window_drag(false);
    
	//join game menu
	gui.create_window("", "skins/menu1.skn", width/2-win1_w/2, height/2-win1_h/2, win1_w, win1_h);
	gui.windows[1].add_textarea("Your Name:", win1_w/2-75, win1_h-40);
	gui.windows[1].add_textbox(150, 16, win1_w/2-75, win1_h-60, tb_rgb);
	gui.windows[1].add_textarea("Host's IP address:", win1_w/2-75, win1_h-100);
	gui.windows[1].add_textbox(150, 16, win1_w/2-75, win1_h-120, tb_rgb);
	butid = gui.windows[1].add_button("  Join", 55, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::joinOKButtonFunc;
    butid = gui.windows[1].add_button(" Cancel", 130, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);  
	   butFuncs[butid] = &TitleMenu::cancelButtonFunc;
	gui.windows[1].set_visibility(false);
    gui.windows[1].set_window_drag(false);

	//host game menu
	gui.create_window("", "skins/menu1.skn", width/2-win1_w/2, height/2-win1_h/2, win1_w, win1_h);
	gui.windows[2].add_textarea("Your Name:", win1_w/2-75, win1_h-40);
	gui.windows[2].add_textbox(150, 16, win1_w/2-75, win1_h-60, tb_rgb);
	//gui.windows[2].add_textarea("Host's IP address:", win1_w/2-75, win1_h-100);
	//gui.windows[2].add_textbox(150, 16, win1_w/2-75, win1_h-120, tb_rgb);	
	butid = gui.windows[2].add_button("  Game Options", 72, win1_h-120, 105, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::gameOptionsButtonFunc;
	butid = gui.windows[2].add_button("  Start", 55, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::hostOKButtonFunc;
    butid = gui.windows[2].add_button(" Cancel", 130, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);  
	   butFuncs[butid] = &TitleMenu::cancelButtonFunc;
	gui.windows[2].set_visibility(false);
    gui.windows[2].set_window_drag(false);


    //main options menu
	gui.create_window("", "skins/menu1.skn", width/2-win1_w/2, height/2-win1_h/2, win1_w, win1_h);
	gui.windows[3].add_textarea("Resolution:", 20, win1_h-40);
	gui.windows[3].add_textarea((char*)screenResolutions[curResolution].c_str(), 118, win1_h-40);
	butid = gui.windows[3].add_button("+", 195, win1_h-40, 15, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::incResButtonFunc;
    butid = gui.windows[3].add_button("-", 100, win1_h-40, 15, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::decResButtonFunc;
	
	butid = gui.windows[3].add_button("  Apply", 55, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::optionsOKButtonFunc;
    butid = gui.windows[3].add_button(" Cancel", 130, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);  
	   butFuncs[butid] = &TitleMenu::cancelButtonFunc;
	gui.windows[3].set_visibility(false);
    gui.windows[3].set_window_drag(false);

	//host game options menu
	gui.create_window("", "skins/menu1.skn", width/2-win1_w/2, height/2-win1_h/2, win1_w, win1_h);
	gui.windows[4].add_textarea("Moons:", 20, win1_h-40);	
	char temp[50];
	sprintf(temp, "%d", numMoons);
	gui.windows[4].add_textarea(temp, 90, win1_h-40);
	butid = gui.windows[4].add_button("+", 120, win1_h-40, 15, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::incMoonButtonFunc;
    butid = gui.windows[4].add_button("-", 65, win1_h-40, 15, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::decMoonButtonFunc;
	butid = gui.windows[4].add_button("  Apply", win1_w/2-30, win1_h-170, 60, 15, "btu.tga", "btd.tga", NULL);
	   butFuncs[butid] = &TitleMenu::gameOptionsOKButtonFunc;

	gui.windows[4].set_visibility(false);
    gui.windows[4].set_window_drag(true);
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::hostGameButtonFunc(void*)
{
	gui.windows[0].set_visibility(false);
	gui.windows[2].set_visibility(true);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::joinGameButtonFunc(void*)
{
	gui.windows[0].set_visibility(false);
	gui.windows[1].set_visibility(true);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::optionsButtonFunc(void*)
{
	gui.windows[0].set_visibility(false);
	gui.windows[3].set_visibility(true);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::cancelButtonFunc(void*)
{
	gui.windows[0].set_visibility(true);
	gui.windows[1].set_visibility(false);
	gui.windows[2].set_visibility(false);
	gui.windows[3].set_visibility(false);
	gui.windows[4].set_visibility(false);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::joinOKButtonFunc(void* pbut)
{
	gi.playerName = gui.windows[1].textbs[0].text;
	gi.IP = gui.windows[1].textbs[1].text;
	gi.isHost = false;
	killWindow();
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::hostOKButtonFunc(void* pbut)
{
	gi.playerName = gui.windows[2].textbs[0].text;
	gi.isHost = true;
	killWindow();
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::optionsOKButtonFunc(void* pbut)
{
	switch (curResolution){
		case 0:
			gi.width = 800;
			gi.height = 600;
			break;
		case 1:
			gi.width = 1024;
			gi.height = 768;
			break;
		case 2:
			gi.width = 1280;
			gi.height = 1024;
			break;
		case 3:
			gi.width = 1600;
			gi.height = 1200;
			break;
	}

	gui.windows[0].set_visibility(true);
	gui.windows[1].set_visibility(false);
	gui.windows[2].set_visibility(false);
	gui.windows[3].set_visibility(false);
	
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::gameOptionsButtonFunc(void*)
{
	gui.windows[2].set_visibility(false);
	gui.windows[4].set_visibility(true);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::incResButtonFunc(void*)
{
	if (curResolution < 3)
	   curResolution++;

	gui.windows[3].set_textarea_text(1, (char*)screenResolutions[curResolution].c_str());
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::decResButtonFunc(void*)
{
	if (curResolution > 0)
	   curResolution--;

	gui.windows[3].set_textarea_text(1, (char*)screenResolutions[curResolution].c_str());
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::decMoonButtonFunc(void*)
{
	if (numMoons > 0)
	   numMoons--;

	char temp[50];
	sprintf(temp, "%d", numMoons);
	gui.windows[4].set_textarea_text(1, temp);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::incMoonButtonFunc(void*)
{
	numMoons++;

	char temp[50];
	sprintf(temp, "%d", numMoons);
	gui.windows[4].set_textarea_text(1, temp);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::gameOptionsOKButtonFunc(void*)
{
	gi.moons = numMoons;	
	gui.windows[4].set_visibility(false);
	gui.windows[2].set_visibility(true);
	return NULL;
}

/*----------------------------------------------------------------------------*/
void* TitleMenu::quitButtonFunc(void*)
{
	exit(0);
	return NULL;
}



